<script>
export default {
  onLaunch: function(options = {}) {
    const that = this
    let fc = ''
    // #ifdef H5
    fc = that.$f.getUrlParam('fc')
    // #endif
    // #ifdef MP-WEIXIN
    if (options.query?.fc) {
      fc = options.query.fc
    } else if (options.scene.toString().includes('fc')) {
      const scene = this.$f.getSceneParam(options.scene)
      fc = scene.fc
    }
    // #endif
    if (fc) {
      this.$f.cache('fc', fc, 60 * 60 * 24)
    }
		
    that.getGlobal()
		
    if (that.$f.isWx()) {
      that.wxConfig()
    }
		
    let token = that.$f.cache('token')
    
    if (token) {
      // store状态管理 - 存储 token
      that.$f.storeToken(token)
    } else if (that.$f.isMp()) {
      that.mpLogin()
    }
		
    // #ifdef H5
    that.setConsole()
    // #endif
  },
  onShow: function() {
    
  },
  watch: {
    '$store.state.token': {
      handler(val) {
        if (val) {
          this.shareLog()
        }
      },
      immediate: true
    }
  },
  methods: {
    shareLog() {
      let fc = this.$f.cache('fc')
      if (fc) {
        const [_fc, type, objectId] = fc.split('-')
        const param = {
          fc: _fc
        }
        if (objectId) {
          param.type = type
          param.object_id = objectId
        }
        this.$f.post('common/shareLog', param).then(res => {
          if (res.code === 0) {
            this.$f.cache('fc', null)
          }
        })
      }
    },
    setConsole() {
      if (this.$f.getUrlParam('bug')) {
        var script = window.document.createElement('SCRIPT')
        script.src = 'https://cdn.bootcdn.net/ajax/libs/vConsole/3.3.4/vconsole.min.js'
        script.onload = function() {
          window.vConsole = new window.VConsole({
            maxLogNumber: 1000,
            // disableLogScrolling: true,
            onReady: function() {
              console.log('vConsole is ready.')
            },
            onClearLog: function() {
              console.log('on clearLog')
            }
          })
        }
        document.documentElement.appendChild(script)
      }
    },
    mpLogin() {
      this.$f.wxLogin().then(res => {
        this.$f.post('app/miniAuth', { code: res.code }).then(res => {
          this.$f.storeToken(res.data.token)
        })
      })
    },
    wxConfig() {
      const url = window.location.href.split('#')[0]
      this.$f.post('weChat/sdk', { url }).then(res => this.$f.wxConfig(res.data))
    },
    getGlobal() {
      this.$f.get('app/global').then(res => {
        this.$store.commit('setState', {
          config: res.data.config,
          category: res.data.cateList
        })
      })
    }
  }
}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import '@/uni_modules/uni-scss/index.scss';
	@import '@/static/css/unocss/index.scss';
	@import '@/static/css/index.scss';
	/* #ifndef APP-NVUE */
	@import '@/static/customicons.css';
	/* #endif */
	/* #ifndef H5 */
	@import '@/static/iconfont/iconfont.css';
	/* #endif */
	
	// 设置整个项目的背景色
	page {
		font-size: 28rpx;
		line-height: 1.4;
		background-color: #F6F6FA;
	}
	.button-hover[type=warn] {
		opacity: .8;
		color: #fff;
		background-color: $uni-error;
	}

	uni-button, button {
		margin-left: 0;
		margin-right: 0;
		line-height: 80rpx;
		font-size: 32rpx;
		&.round {
			@extend .flex-ca;
			border-radius: 999px;
			&::after {
				border-radius: 999px;
			}
		}
		&[disabled][type=primary] {
			background-color: $uni-primary;
		}
		&[type=primary] {
			color: #fff;
			background-color: $uni-primary;
		}
		&[type=warn] {
			color: #fff;
			background-color: $uni-error;
		}
		&[size=mini] {
			font-size: 24rpx;
			line-height: 52rpx;
		}
		&[plain] {
			color: $uni-primary;
			border-color: $uni-primary;
			// background-color: #EBF4FF;
			// background-color: transparent;
					
			&::after {
				border-color: $uni-primary;
			}
		}
	}
	
</style>
